package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.Category;
import com.ktmall.ktmalluijava.service.CategoryService;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/14 下午1:32
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/list")
    public String select(String name){
        List<Category> categoryList = categoryService.getByName(name);
        return ResMsg.success("分类列表加载成功",categoryList);
    }

    @PostMapping("/add")
    public String add(@RequestBody Category category){
        int i = categoryService.add(category);
        if(i>0){
            return ResMsg.success("新增成功");
        }else{
            return ResMsg.error("新增失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id){
        Category category ;
        try{
            category = categoryService.detail(id);
        }catch (Exception e){
            return ResMsg.error("分类详情加载失败",e);
        }
        return ResMsg.success("分类详情加载成功",category);
    }

    @PostMapping("/update")
    public String update(@RequestBody Category category){
        int i = categoryService.update(category);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }

    @DeleteMapping("/del")
    public String delete(Integer id){
        int i = categoryService.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return ResMsg.error("删除失败");
        }
    }

    @GetMapping("/levelList")
    public String levelList(Integer categoryLevel){
        List<Category> categoryList = categoryService.getByCategoryLevel(categoryLevel);
        return ResMsg.success("分类列表加载成功",categoryList);
    }

    @GetMapping("/count")
    public String count(){
        return ResMsg.success("分类层数加载成功过", categoryService.count());
    }
}
